﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Arbalest.Web.Services;
using Arbalest.Models;
using Arbalest.Web.Helpers;

namespace Arbalest.Web.Controllers
{
    public class ItemController : Controller
    {
        //
        // GET: /Item/

        public ActionResult Index()
        {
            DataStore ds = new DataStore();
            return View(ds.GetList<Item>());
        }

        //
        // GET: /Company/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Company/Create

        public ActionResult Create()
        {
            DataStore ds = new DataStore();

            TempData["CompanyId"] = new SelectList(
                ds.GetList<Company>(),
                "Id",
                "Name");

            //TempData["StorageLocationTypeId"] = new SelectList(
            //    ds.GetList<StorageLocationType>(),
            //    "Id",
            //    "Code");

            return View();
        }

        //
        // POST: /Company/Create

        [HttpPost]
        public ActionResult Create(Item item, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();
                ds.Add<Item>(item);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record added."
                };

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Company/Edit/5

        public ActionResult Edit(int id)
        {
            DataStore ds = new DataStore();

            Item item = ds.GetList<Item>().Where(rec => rec.Id == id).FirstOrDefault();

            TempData["CompanyId"] = new SelectList(
                ds.GetList<Company>(),
                "Id",
                "Name", item.CompanyId);

            return View(item);
        }

        //
        // POST: /Company/Edit/5

        [HttpPost]
        public ActionResult Edit(Item item, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();
                ds.Update<Item>(item);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record updated."
                };

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Company/Delete/5

        public ActionResult Delete(int id)
        {
            DataStore ds = new DataStore();

            return View(ds.GetList<Item>().Where(rec => rec.Id == id).FirstOrDefault());
        }

        //
        // POST: /Company/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();

                Item item = 
                    ds.GetList<Item>().Where(rec => rec.Id == id).FirstOrDefault();
                ds.Remove<Item>(item);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record removed."
                };

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        //
        // GET: /Company/Create/1

        public ActionResult CreateChild(int id)
        {
            // Create children of given id
            DataStore ds = new DataStore();

            StorageLocation storageLocation = new StorageLocation();
            StorageLocation storageLocationParent = ds.GetList<StorageLocation>()
                .Where(rec => rec.Id == id).FirstOrDefault();

            if (storageLocationParent != null)
            {
                if (storageLocationParent.CompanyId.HasValue)
                {
                    storageLocation.StorageLocationParent = storageLocationParent;
                    storageLocation.CompanyId = storageLocationParent.CompanyId;
                    TempData["SelectedCompanyName"] = ds.GetList<Company>()
                        .Where(rec => rec.Id == storageLocationParent.CompanyId)
                        .FirstOrDefault().Name;
                }
            }
            else
            {
                TempData["CompanyId"] = new SelectList(
                    ds.GetList<Company>(), "Id", "Name");
            }

            return View(storageLocation);
        }

    }
}
